/*
Janet Putnam
CS 362
Assignment 3
Unit Test 3 - Unit Test of updateCoins

Include the following lines in make file:

unittest3:  unittest3.c dominion.o rngs.o
	gcc -o unittest3 -g unittest3.c dominion.o rngs.o $(CFLAGS)

*/

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

/* Copy of function from dominion.c for reference purposes, can be removed from code
int updateCoins(int player, struct gameState *state, int bonus)
{
  int i;
	
  //reset coin count
  state->coins = 0;

  //add coins for each Treasure card in player's hand
  for (i = 0; i < state->handCount[player]; i++)
    {
      if (state->hand[player][i] == copper)
	{
	  state->coins += 1;
	}
      else if (state->hand[player][i] == silver)
	{
	  state->coins += 2;
	}
      else if (state->hand[player][i] == gold)
	{
	  state->coins += 3;
	}	
    }	

  //add bonus
  state->coins += bonus;

  return 0;
}
*/
int updateCoins(int player, struct gameState *state, int bonus);

int updateCoinsTest(int print_pass)
{
	int result;
	int r1;
	int r2;
	int r3;
	int i;
	int j;
	int l;
	int sum;
	int player;
	//setup initial struct gameState
	struct gameState G;
	memset(&G, 0, sizeof(struct gameState));
	
	//select cards in game
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};

	//initialize gameState struct
	initializeGame(2, k, 4, &G);
	
	for(l=0; l<10; l++)
	{
		printf("Test %d:  \n", l);
	
		//randomly set player info
		for(i=0; i<2; i++)
		{
			sum = 0;
			r1 = (rand() % 13);
			r2 = (rand() % 5);
			r3 = 0;
			//r3 = (rand() % 11);
			player = i;
			G.handCount[player] = r1;
			for(j=0; j<r1; j++)
			{
				if(r2 == 0)
				{
					G.hand[player][j] = gold;
					sum = sum + 3;
				}
				else if(r2 == 1)
				{
					G.hand[player][j] = silver;
					sum = sum + 2;
				}
				else if(r2 == 2)
				{
					G.hand[player][j] = copper;
					sum = sum + 1;
				}
				else
				{
					G.hand[player][j] = smithy;
				}
				
				sum = sum + r3;
				
				result = updateCoins(i, &G, r3);
				
				//verify coin count was done correctly
				if(sum == (G.coins))
				{
						if(print_pass == 0)
						{
							printf("PASS:  Number of coins added correctly.  %d counted, %d added.\n", sum, (G.coins));
						}
				}
				else
				{
					printf("FAIL:  Number of coins not added correctly.  %d counted, %d added.\n", sum, (G.coins));			
				}
				
				if(result == 0)
				{
					if(print_pass == 0)
						{
							printf("PASS:  Function ended correctly.\n");
						}
					
				}
				else
				{
					printf("Fail:  Function did not end correctly.\n");
				}
			}
		}

	}
	
	return 0;
}

int main(int argc, char **argv)
{	
	int print_pass;
	//if print_pass = 0, then results will print for passing tests.
	//if print_pass = 1, then results will not print for passing tests.
	//regardless of print_pass state, failing tests will print results.
	
	print_pass = atoi(argv[1]);
	if ((print_pass != 1 ) || (print_pass != 0))
	{
		print_pass = 0;
	}
	srand(time(NULL));
	updateCoinsTest(print_pass);
	return 0;
}